package edu.study.user.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionTemplate;
import org.springframework.util.CollectionUtils;

import edu.study.api.common.dto.BaseRespDto;
import edu.study.api.common.dto.RespDto;
import edu.study.api.common.enums.BaseResultCodeEnum;
import edu.study.user.api.dto.RoleUserDto;
import edu.study.user.api.dto.UserDetailDto;
import edu.study.user.api.req.roleuser.RoleUserReq;
import edu.study.user.api.req.roleuser.QueryRoleUserReq;
import edu.study.user.dao.mapperExt.RoleDOMapperExt;
import edu.study.user.dao.mapperExt.RoleUserDOMapperExt;
import edu.study.user.dao.mapperExt.UserDOMapperExt;
import edu.study.user.dbBean.RoleDO;
import edu.study.user.dbBean.RoleUserDO;
import edu.study.user.dbBean.UserDO;
import edu.study.user.util.UserUtils;
import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class RoleUserService {
	
	@Autowired
	private RoleUserDOMapperExt roleUserDOMapperExt;

	@Autowired
	private RoleDOMapperExt roleDOMapperExt;
	
	@Autowired
	private UserDOMapperExt userDOMapperExt;
	
	@Autowired
	private TransactionTemplate transactionTemplate;

	public BaseRespDto addRoleUser(RoleUserReq req) {
		BaseRespDto resp = new BaseRespDto();
		
		RoleDO roleDO = roleDOMapperExt.queryByRoleNo(req.getRoleNo());
		if(roleDO == null) {
			log.info("RoleUserService.addRoleUser, 角色不存在! roleNo={}", req.getRoleNo());
			resp.setResultFail(BaseResultCodeEnum.RECORD_NOT_FOUND);
			return resp;
		}
		
		List<RoleUserDO> roleUserDOList = new ArrayList<RoleUserDO>(req.getUserNoList().size());
		for (String userNo : req.getUserNoList()) {
			UserDO userDO = userDOMapperExt.queryByUserNo(userNo);
			if(userDO == null) {
				log.info("RoleUserService.addRoleUser, 用户不存在! userNo={}", userNo);
				resp.setResultFail(BaseResultCodeEnum.RECORD_NOT_FOUND);
				return resp;
			}
			RoleUserDO newRoleUserDO = new RoleUserDO();
			newRoleUserDO.setRoleNo(req.getRoleNo());
			newRoleUserDO.setUserNo(userNo);
			UserDetailDto userDetailDto = UserUtils.getCurrentUser();
			if(userDetailDto!=null) {
				newRoleUserDO.setCreator(userDetailDto.getLoginName());
			}
			roleUserDOList.add(newRoleUserDO);
		}
		
		Boolean flag = transactionTemplate.execute(new TransactionCallback<Boolean>() {
			@Override
			public Boolean doInTransaction(TransactionStatus arg0) {
				for (RoleUserDO roleUserDO : roleUserDOList) {
					roleUserDOMapperExt.insertSelective(roleUserDO);
				}
				return true;
			}
		});
		if (!flag) {
			resp.setResultFail(BaseResultCodeEnum.DB_ERROR);
			return resp;
		}
		return resp;
	}
	
	public BaseRespDto deleteRoleUser(RoleUserReq req) {
		BaseRespDto resp = new BaseRespDto();
		List<RoleUserDto> roleUserListAll = new ArrayList<RoleUserDto>(req.getUserNoList().size());
		for (String userNo : req.getUserNoList()) {
			List<RoleUserDto> roleUserList = roleUserDOMapperExt.queryRoleUserList(req.getRoleNo(), userNo);
			if(CollectionUtils.isEmpty(roleUserList)) {
				log.info("RoleUserService.deleteRoleUser, 角色用户不存在! roleNo={}, userNo={}", req.getRoleNo(), userNo);
				resp.setResultFail(BaseResultCodeEnum.RECORD_NOT_FOUND);
				return resp;
			}
			roleUserListAll.addAll(roleUserList);
		}
		Boolean flag = transactionTemplate.execute(new TransactionCallback<Boolean>() {
			@Override
			public Boolean doInTransaction(TransactionStatus arg0) {
				for (RoleUserDto roleUserDto : roleUserListAll) {
					roleUserDOMapperExt.deleteByPrimaryKey(roleUserDto.getId());
				}
				return true;
			}
		});
		if (!flag) {
			resp.setResultFail(BaseResultCodeEnum.DB_ERROR);
			return resp;
		}
		return resp;
	}
	
	public BaseRespDto modifyRoleUser(RoleUserReq req) {
		BaseRespDto resp = new BaseRespDto();
		
		RoleDO roleDO = roleDOMapperExt.queryByRoleNo(req.getRoleNo());
		if(roleDO == null) {
			log.info("RoleUserService.modifyRoleUser, 角色不存在! roleNo={}", req.getRoleNo());
			resp.setResultFail(BaseResultCodeEnum.RECORD_NOT_FOUND);
			return resp;
		}
		
		List<RoleUserDO> roleUserDOListNew = new ArrayList<RoleUserDO>(req.getUserNoList().size());
		for (String userNo : req.getUserNoList()) {
			UserDO userDO = userDOMapperExt.queryByUserNo(userNo);
			if(userDO == null) {
				log.info("RoleUserService.modifyRoleUser, 用户不存在! userNo={}", userNo);
				resp.setResultFail(BaseResultCodeEnum.RECORD_NOT_FOUND);
				return resp;
			}
			RoleUserDO newRoleUserDO = new RoleUserDO();
			newRoleUserDO.setRoleNo(req.getRoleNo());
			newRoleUserDO.setUserNo(userNo);
			UserDetailDto userDetailDto = UserUtils.getCurrentUser();
			if(userDetailDto!=null) {
				newRoleUserDO.setCreator(userDetailDto.getLoginName());
			}
			roleUserDOListNew.add(newRoleUserDO);
		}
		List<RoleUserDto> roleUserListOld = roleUserDOMapperExt.queryRoleUserList(req.getRoleNo(), null);
		Boolean flag = transactionTemplate.execute(new TransactionCallback<Boolean>() {
			@Override
			public Boolean doInTransaction(TransactionStatus arg0) {
				if(!CollectionUtils.isEmpty(roleUserListOld)) {
					for (RoleUserDto roleUserDto : roleUserListOld) {
						roleUserDOMapperExt.deleteByPrimaryKey(roleUserDto.getId());
					}
				}
				for (RoleUserDO roleUserDO : roleUserDOListNew) {
					roleUserDOMapperExt.insertSelective(roleUserDO);
				}
				return true;
			}
		});
		if (!flag) {
			resp.setResultFail(BaseResultCodeEnum.DB_ERROR);
			return resp;
		}
		return resp;
	}
	
	public RespDto<List<RoleUserDto>> queryRoleUserList(QueryRoleUserReq req) {
		RespDto<List<RoleUserDto>> resp = new RespDto<List<RoleUserDto>>();
		List<RoleUserDto> roleUserList = roleUserDOMapperExt.queryRoleUserList(req.getRoleNo(), req.getUserNo());
		resp.setData(roleUserList);
		return resp;
	}
}
